<template>
    <div class="content">
        <div class="clearfix">
            <Form class="f-fl" inline :label-width="5">
                <FormItem>
                    <Input
                        v-model="params.title"
                        placeholder="公告标题"
                    ></Input>
                </FormItem>
                <FormItem>
                    <Select
                        style="width: 140px;"
                        placeholder="状态"
                        v-model="params.status"
                        clearable
                    >
                        <Option :value="1">有效</Option>
                        <Option :value="0">失效</Option>
                    </Select>
                </FormItem>
                <FormItem>
                    <Button type="primary" @click="search">搜索</Button>
                </FormItem>
                <FormItem>
                    <Button @click="clearSearch">清空</Button>
                </FormItem>
            </Form>
            <div class="f-fr">
                <Button
                    type="primary"
                    @click="handleAdd"
                >新建公告</Button>
            </div>
        </div>
        <Table :data="list" :columns="columns" border align="center">
            <template slot-scope="{ row }" slot="title">
                <div>{{ row.title }}</div>
            </template>
            <template slot-scope="{ row }" slot="valid">
                <div>{{ row.valid ? "有效" : "失效" }}</div>
            </template>
            <template slot-scope="{ row }" slot="publishTime">
                <div>{{ row.publishTime }}</div>
            </template>
            <template slot-scope="{ row }" slot="action">
                <div class="table-action">
                    <Button
                        type="text"
                        @click="handleEdit(row.noticeId)"
                    >编辑</Button>
                    <Button
                        type="text"
                        @click="handleDelete(row.noticeId)"
                    >删除</Button>
                </div>
            </template>
        </Table>
        <Pagination
            :pageSize.sync="params.pageSize"
            :current.sync="params.pageNum"
            :total="params.total"
            @change="queryList"
        />
    </div>
</template>
<script>
import { queryNoticeList, deleteNotice } from '@/api/notice'
import formatTime from '@/libs/formatTime'

export default {
    inject: ['nextPageReload'],
    data () {
        return {
            params: {
                total: 0,
                status: '',
                pageNum: 1,
                pageSize: 10,
                title: ''
            },
            list: [],
            columns: Object.freeze([
                { title: '序号', type: 'index', align: 'center', width: 80 },
                { title: '公告标题', slot: 'title', align: 'center' },
                { title: '发布时间', slot: 'publishTime', align: 'center' },
                { title: '状态', slot: 'valid', align: 'center' },
                { title: '操作', slot: 'action', align: 'center', width: 100 }
            ])
        }
    },
    mounted () {
        this.queryList()
    },
    methods: {
        search () {
            this.params.pageNum = 1
            this.queryList()
        },
        queryList () {
            const valid =
                typeof this.params.status !== 'number'
                    ? ''
                    : this.params.status === 1
            queryNoticeList({
                ...this.params,
                valid
            }).then(res => {
                if (res.data.code === '0000') {
                    const data = res.data.data
                    data.list.forEach(item => {
                        item.publishTime = formatTime.fullTime(item.publishTime)
                    })
                    this.params.total = data.total
                    this.list = data.list
                } else {
                    this.$Message.warning(res.data.msg)
                }
            })
        },
        clearSearch () {
            this.params = {
                total: 0,
                status: '',
                pageNum: 1,
                pageSize: 10,
                title: ''
            }
            this.queryList()
        },
        handleDelete (id) {
            this.$Modal.confirm({
                title: '提示',
                content: '确定删除该公告?',
                onOk: () => {
                    deleteNotice(id).then(res => {
                        if (res.data.code === '0000') {
                            this.$Message.success('删除成功')
                            this.params.pageNum = 1
                            this.queryList()
                        } else {
                            this.$Message.error(res.data.msg)
                        }
                    })
                }
            })
        },
        handleAdd () {
            this.nextPageReload()
            this.$router.push({
                path: '/noticeManage/add'
            })
        },
        handleEdit (id) {
            this.nextPageReload()
            this.$router.push({
                name: 'noticeManageUpdate',
                params: {
                    id
                }
            })
        }
    }
}
</script>
